//
//  ActivitiesViewController.m
//  SocialEngine
//
//  Created by Tan NguyenBa on 9/29/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "ActivitiesViewController.h"
#import "PageSetting.h"
#import "WebServices.h"
#import "XMLParser.h"

@implementation ActivitiesViewController {
    // Variable control pageId, itemPerPage value
    PageSetting *pageSetting;
    
    // Variables control reloading action
    PullToReloadView *refreshHeaderView;
    BOOL _reloading;
}

// Init function
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

// Memory function
- (void) didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [self setTableActivity:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) viewWillAppear:(BOOL)animated {
    // Init value for page
    if (!pageSetting) {
        pageSetting = [[PageSetting alloc] init];
    }
    [pageSetting initpageValue];
    
    // Init header,  use for reload
    if (refreshHeaderView == nil) {
		PullToReloadView *view = [[PullToReloadView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableActivity.bounds.size.height, self.view.frame.size.width, self.tableActivity.bounds.size.height)];
		view.delegate = self;
		[self.tableActivity addSubview:view];
		refreshHeaderView = view;
	}
    
    [refreshHeaderView refreshLastUpdatedDate];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - 
#pragma mark ScrollView delegate method

// Action control when user scroll view
- (void) scrollViewDidScroll:(UIScrollView *) scrollView {
	[refreshHeaderView disposeScrollViewDidScroll:scrollView];
}

- (void) scrollViewDidEndDragging:(UIScrollView *) scrollView willDecelerate:(BOOL) decelerate{
	[refreshHeaderView disposeScrollViewDidEndDragging:scrollView];
}

#pragma mark -
#pragma mark Reload data methods

- (void) reloadingData
{
    // Begin here
    // ...
    /*
    NSString *strUrlHttp = [NSString stringWithFormat:@"%@/user/login?ObjectType=email&ObjectId=%@&Password=%@&remember=1",
                            URL_MAIN, @"abc", @"def"];
    NSLog(@"%@", strUrlHttp);
    NSData *responseData = [WebServices loginWithURL:strUrlHttp];
    
    if (responseData) {
        XMLParser *parse = [[XMLParser alloc] initXMLParser:PARSE_LOGIN];
		[parse doParse:responseData];
    }
    */
	
    [self performSelectorOnMainThread:@selector(doneGetData) withObject:nil waitUntilDone:NO];
}

- (void) doneGetData {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    _reloading = NO;
	[refreshHeaderView actionScrollViewDidFinishedLoading:self.tableActivity];
}

#pragma mark - 
#pragma mark PullToReload delegate method

- (void) startRefreshAction:(PullToReloadView *) view {
    _reloading = YES;
    
    // Start reload activity data
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self performSelectorInBackground:@selector(reloadingData) withObject:nil];
}

- (NSDate *) refreshTableHeaderLastUpdated:(PullToReloadView *) view{
	return [NSDate date];	
}

- (BOOL) detectTableIsLoading:(PullToReloadView *) view {
    return _reloading;
}

#pragma mark -
#pragma mark Tableview delegate, datasource methods

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
	// Configure the cell.
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	return [NSString stringWithFormat:@"Section %i", section];	
}

@end
